import 'dart:async';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/common/class/eventBus/index.dart';
import 'package:qgchat/http/models/socketModel.dart';

class ChatingBloc implements BlocBase {

  ChatingBloc() {
    bus.on('listenMessage', (data) {
      SocketModel msgData = (data as SocketModel);
      listenMessage(msgData);
    });
  }

  // ##########  VARIABLE  ##############

  List<SocketModel> chatList = [];

  // ##########  DEFAULT  ##############

  /// 私聊列表默认值
  final List<SocketModel> chatListDefault = [];

  // ##########  STREAMS  ##############

  /// stream
  StreamController _chat = StreamController<List<SocketModel>>.broadcast();
  Sink get _chatSink => _chat.sink;
  Stream get chatStream => _chat.stream;

  void dispose() {
    _chat.close();
  }

  // ##########  HANDLING  ##############

  /// 监听消息
  void listenMessage(SocketModel data) {
    chatList.add(data);
    _chatSink.add(chatList);
  }
}
